<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ajax</title>
</head>
<body>
    <button id="btn1">axios请求</button>
    <button id="btn2">jquery请求</button>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.2/jquery.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>


var service = axios.create({
    baseURL: 'https://some-domain.com/api/', // 初始 baseURL，可在业务里用绝对地址替换
    method: 'post',                          // 可在业务里替换
    timeout: 8000,                           // 可在业务里替换
    headers: {'Authorization': localStorage.getItem('access-token') || 'noToken'}
});

// request 拦截器
service.interceptors.request.use(config => {
    return config
}, error => Promise.reject(error));


// respone拦截器
service.interceptors.response.use(
    response => {
        const res = response.data;
        switch (res.errNo) {
            case 0 :
                return res;
            break;
            case 401 :
                // 做一些事情，比如弹到登录页
                alert('登录状态失效，请重新登录');
            break;
            default :
                alert('其他错误', JSON.stringify(res));
            break;
        }
    },
    error => Promise.reject(error)
);


    var data = {"access-token":"bll34lnkih21an6dr6g13fo5a3","page-token":"bll34lnkih21an6dr6g13fo5a3"}
    var url1 = 'http://shop.mwee.cn/msk/api/mwcenter.shopAccount.getAccountInfo';
    var url2 = 'http://127.0.0.1:3003/api/v1/login'; // 填写绝对地址会替换掉 baseURL
    var url3 = 'http://127.0.0.1:3003/api/v1/getInfo';

    $("#btn1").click(function() {
        service({
            url: url3,
            data: data,
            // headers: { 'Content-Type': 'multipart/form-data' } // 上传文件
        }).then(res => console.log(res)).catch(err => console.log(err));
    });


    $("#btn2").click(function() {
        $.ajax({
            url: url1,
            method: 'post',
            data: data,
            success: function(data) {
                console.log(data);
            },
            error: function(err) {
                console.log(err);
            }
        })
    });


</script>
</html>